<?php
namespace app\api\model\goods;
use think\facade\Db;
class Goods{
	
	public function findType($_where,$_field){
		return Db::name("shop_type")->where($_where)->field($_field)->find();
	}
	
	public function selectType($_where,$_field,$_limit,$_order)
	{
		$_data=Db::name("shop_type")->where($_where)->field($_field)->limit(current($_limit),next($_limit))->order($_order)->select();
		return empty($_data)?:$_data->toArray();
	}
	
	public function findGoods($_where,$_field){
		return Db::name("shop_goods")->where($_where)->field($_field)->find();
	}
	
	public function selectGoods($_where,$_field,$_limit=[0,20],$_order="id asc")
	{
		$_data=Db::name("shop_goods")->where($_where)->field($_field)
				->limit(current($_limit),end($_limit))
				->order($_order)
				->select();
		return empty($_data)?:$_data->toArray();			
	}
	public function findGoodsData($_where,$_field){
		return Db::name("shop_goods")->alias("g")
			->join("shop_type t","g.type_id=t.id")->where($_where)->field($_field)->find();
	}
	
	public function selectGoodsData($_where,$_field,$_limit=[0,20],$_order="g.id asc")
	{
		$_data=Db::name("shop_goods")->alias("g")
				->join("shop_type t","g.type_id=t.id")
				->where($_where)
				->limit(current($_limit),end($_limit))
				->order($_order)
				->field($_field)
				->select();
		return empty($_data)?:$_data->toArray();			
	}
	
	public function selectGoodsDetails($_where,$_field){
		$_data=Db::name("shop_goods_details")->where($_where)->field($_field)->select();
		return empty($_data)?:$_data->toArray();
	}
	
	public function updateGoodsStock($_up,$_where){
		return Db::name("shop_goods")->dec(current($_up['dec']),end($_up['dec']))->inc(current($_up['inc']),end($_up['inc']))->where($_where)->update();
	}
	
}
?>